//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (c) 1995-2000 Microsoft Corporation.  All rights reserved.

Module Name:  

bvd_ser16550.h

Abstract:

Definitions for Bulverde's serial 16550 library

Notes: 


--*/
#ifndef __BVD_SER16550_H__
#define __BVD_SER16550_H__

#ifdef __cplusplus
extern "C" {
#endif

// We use a callback for serial events
typedef Void  (*EVENT_FUNC)(PVoid Arg1, UInt32 Arg2);

// The library has a default baud table, but this can be replaced
typedef struct __XSC1_PAIRS
{
    UInt32   Key;
    UInt32   AssociatedValue;
} XSC1_PAIRS, *PXSC1_PAIRS;

typedef struct __LOOKUP_TBL
{
    UInt32        Size;
    PXSC1_PAIRS  Table;
} LOOKUP_TBL, *PLOOKUP_TBL;

//
// @doc HWINTERNAL
// @struct LS_SERIAL_INFO | Passed to serial lib routines.
//

typedef struct __LS_SERIAL_INFO
{
    //Even though Cotulla UART registers are 32 bit, only the least significant byte is used
    //The other three bytes are reserved. But still, we do 32 bit reads/writes.
    volatile PUInt32    pTHR_RBR_DLL;     // @field DLAB = 0  WO  32bit - Transmit Holding Register (THR)
                                         // @field DLAB = 0  RO  32bit - Recieve Buffer Register (RBR)
                                         // @field DLAB = 1  RW  32bit - Divisor Latch Low Register (DLL)
    volatile PUInt32    pIER_DLH;         // @field DLAB = 0  RW  32bit - Interrupt Enable Register
                                         // @field DLAB = 1  RW  32bit - Divisor Latch High Register (DLH)
    volatile PUInt32    pIIR_FCR;         // @field DLAB = X  RO  32bit - Interrupt Identification Register
                                         // @field DLAB = X  WO  32bit - FIFO Control Register
    volatile PUInt32    pLCR;             // @field DLAB = X  RW  32bit - Line Control Register
    volatile PUInt32    pMCR;             // @field DLAB = X  RW  32bit - Modem Control Regiser
    volatile PUInt32    pLSR;             // @field DLAB = X  RO  32bit - Line Status Register
    volatile PUInt32    pMSR;             // @field DLAB = X  RO  32bit - Modem Status Register
    volatile PUInt32    pSCR;             // @field DLAB = X  RW  32bit - Scratchpad Register
    volatile PUInt32    pIRDASEL;         // @field DLAB = X  RW  32bit - IrDA Select Register
    volatile PUInt32    pFOR;             // @field DLAB = X  RO  32bit - FIFO Occupancy Register
    volatile PUInt32    pABR;             // @field DLAB = X  RW  32bit - Autobaud Control Register
    volatile PUInt32    pACR;             // @field DLAB = X      32bit - Autobaud Count Register

    Boolean  bIr;                           // @field device running in normal serial mode or IR mode
    Boolean  PowerDownFlag;                 // @field whether device is powered down or not

    // And we keep shadows of many of the Cotulla UART registers
    UInt32  FCR;            // @field FIFO control state.
    UInt32  IIR;            // @field State of Interrupt Identification Register.
    UInt32  LSR;            // @field Line Status Register.
    UInt32  MSR;            // @field Modem Status Register.
    // We wouldn't normally shadow these, except for power on/off
    UInt32  IER;            // @field Interrupt Enable Register.
    UInt32  LCR;            // @field Line Control Register.
    UInt32  MCR;            // @field Modem Control Register.
    UInt32  SCR;            // @field Scratch Register.
    UInt32  IRDASEL;        // @field Infrared Selection Register.
    UInt32  FOR;            // @field FIFO Occupancy Register
    UInt32  ABR;            // @field Autobaud Control Register
    UInt32  ACR;            // @field Autobaud Count Register

    // We have an event callback into the MDD
    EVENT_FUNC EventCallback; // This callback exists in MDD
    PVoid        pMddHead;   // This is the first parm to callback

    // Keep a copy of DCB since we rely on may of its parms
    DCB         dcb;        // @field Device Control Block (copy of DCB in MDD)
    // And the same thing applies for CommTimeouts
    COMMTIMEOUTS CommTimeouts;  // @field Copy of CommTimeouts structure

    // Misc fields used by ser16550 library
    UInt32       OpenCount;           // @field Count of simultaneous opens.
    PLOOKUP_TBL pBaudTable;          // @field Pointer to Baud Table
    UInt32       DroppedBytes;        // @field Number of dropped bytes
    PVoid      FlushDone;           // @field Handle to flush done event.
    Boolean        CTSFlowOff;          // @field Flag - CTS flow control state.
    Boolean        DSRFlowOff;          // @field Flag - DSR flow control state.
    Boolean        AddTXIntr;           // @field Flag - Fake a TX intr.
    COMSTAT     Status;              // @field Bitfield representing Win32 comm status.
    UInt32       CommErrors;          // @field Bitfield representing Win32 comm error status.
    UInt32       ModemStatus;         // @field Bitfield representing Win32 modem status.
    CRITICAL_SECTION TransmitCritSec;    // @field Protects UART Registers for non-atomic accesses
    CRITICAL_SECTION RegCritSec;         // @field Protects UART
    UInt32       ChipID;             // @field Chip identifier (CHIP_ID_COTULLA or CHIP_ID_16550 or CHIP_ID_16450)

    UInt32       IOBase;          // @field IO Base Address - unmapped (to find out which UART is it)
    UInt32        BLR;            // @field Board level register

    volatile XLLP_GPIO_T *pGPIOReg;       // @field for configuring GPIO pins
    //volatile GPIO_REGS *pGPIOReg;       // @field for configuring GPIO pins
    volatile XLLP_CLKMGR_T *pClkMgrReg;   // @field for configuring clk mgr
    //volatile BLR_REGS      *pBLRReg;    // @field for board specific stuff
    volatile XLLP_BCR_T      *pBCRReg;    // @field for board specific stuff

} SER16550_INFO, *PSER16550_INFO;

// Values for SER16550_INFO.ChipID
#define CHIP_ID_16550  1
#define CHIP_ID_16450  2
#define CHIP_ID_COTULLA  4
#define CHIP_ID_BULVERDE 8

// Here is the callback for serial events
typedef Void (*PFN_SER_EVENT) (
                PVoid pHandle,              // PHW_INDEP_INFO, but pdd doesn't know it
                UInt32 events);               // What events where encountered?

// And now, all the function prototypes
Void HW_XSC1_Init(
                PVoid   pHead,            //  points to device head
                PUInt32  pRegBase,         // Pointer to 16550 register base
                UInt32   RegStride,        // Stride amongst the 16550 registers
                UInt32    IoBase,          // IO Base Address
                volatile XLLP_GPIO_T   *pGPIOReg,   // @field for configuring GPIO pins
                volatile XLLP_CLKMGR_T *pClkMgrReg, // @field for configuring clk mgr
                volatile XLLP_BCR_T    *pBCRReg,    // @field for board specific stuff
                EVENT_FUNC EventCallback, // This callback exists in MDD
                PVoid   pMddHead,         // This is the first parm to callback
                PLOOKUP_TBL pBaudTable);    // Pointer to baud rate table
Boolean HW_XSC1_PostInit(
                PVoid   pHead);
Void HW_XSC1_Deinit(
                PVoid   pHead); //  points to device head
Void HW_XSC1_Open(
                PVoid   pHead);
Void HW_XSC1_Close(
                PVoid   pHead);
Void HW_XSC1_ClearDTR(
                PVoid   pHead);
Void HW_XSC1_SetDTR(
                PVoid   pHead);
Void HW_XSC1_ClearRTS(
                PVoid   pHead);
Void HW_XSC1_SetRTS(
                PVoid   pHead);
Void HW_XSC1_ClearBreak(
                PVoid   pHead);
Void HW_XSC1_SetBreak(
                PVoid   pHead);
Void HW_XSC1_ClearBreak(
                PVoid   pHead);
Void HW_XSC1_SetBreak(
                PVoid   pHead);
UInt32 HW_XSC1_GetByteNumber(
                PVoid   pHead);
Void HW_XSC1_DisableXmit(
                PVoid   pHead);
Void HW_XSC1_EnableXmit(
                PVoid   pHead);
Boolean HW_XSC1_SetBaudRate(
                PVoid   pHead,
                UInt32   BaudRate); //      UInt32 representing decimal baud rate.
Boolean HW_XSC1_SetDCB(
                PVoid   pHead,
                LPDCB   lpDCB);       //     Pointer to DCB structure
UInt32 HW_XSC1_SetCommTimeouts(
                PVoid   pHead,
                LPCOMMTIMEOUTS   lpCommTimeouts); //  Pointer to CommTimeout structure
UInt32 HW_XSC1_GetRxBufferSize(
                PVoid pHead);
PVoid HW_XSC1_GetRxStart(
                PVoid   pHead);
INTERRUPT_TYPE HW_XSC1_GetInterruptType(
                PVoid pHead);
UInt32 HW_XSC1_RxIntr(
                PVoid pHead,
                PUInt8 pRxBuffer,       // Pointer to receive buffer
                UInt32 *pBufflen);         //  In = max bytes to read, out = bytes read
UInt32 HW_XSC1_PutBytes(
                PVoid   pHead,
                PUInt8  pSrc,     //  Pointer to bytes to be sent.
                UInt32   NumberOfBytes,  //  Number of bytes to be sent.
                PUInt32  pBytesSent);     //  Pointer to actual number of bytes put.
Void HW_XSC1_TxIntr(
                PVoid pHead);
Void HW_XSC1_LineIntr(
                PVoid pHead);
Void HW_XSC1_OtherIntr(
                PVoid pHead);
Void HW_XSC1_ModemIntr(
                PVoid pHead);
UInt32 HW_XSC1_GetStatus(
                PVoid pHead,
                LPCOMSTAT lpStat); // Pointer to LPCOMMSTAT to hold status.
Void HW_XSC1_Reset(
                PVoid   pHead);
Void HW_XSC1_GetModemStatus(
                PVoid   pHead,
                PUInt32  pModemStatus);    //  PUInt32 passed in by user.
Void HW_XSC1_PurgeComm(
                PVoid   pHead,
                UInt32   fdwAction);     //  Action to take.
Boolean HW_XSC1_XmitComChar(
                PVoid   pHead,
                UInt8   ComChar);   //  Character to transmit.
Void HW_XSC1_PowerOn(
                PVoid   pHead);
Void HW_XSC1_PowerOff(
                PVoid   pHead);
Boolean HW_XSC1_Ioctl(
                PVoid pHead,
                UInt32 dwCode,
                PByte pBufIn,
                UInt32 dwLenIn,
                PByte pBufOut,
                UInt32 dwLenOut,
                PUInt32 pdwActualOut);

Void HW_XSC1_TxIntrEx(
                PVoid pHead,
                PUInt8 pTxBuffer,          // @parm Pointer to receive buffer
                UInt32 *pBufflen);            // @parm In = max bytes to transmit, out = bytes transmitted
Void HW_XSC1_Enable_Uart_IR(
                PVoid pHead,
                Boolean EnableUart);                // Hardware Head
Boolean HW_XSC1_TransmitterBusy(
                PVoid pHead);
void HW_XSC1_Enable_IR_Rx_Tx(
                PVoid pHead,
                Boolean Rxenable,
                Boolean Txenable);

void HW_XSC1_Enable_IR_Tx(
                PVoid pHead,
                Boolean enable);

void HW_XSC1_Enable_IR_Rx(
                PVoid pHead,
                Boolean enable);

#ifdef __cplusplus
}
#endif

#endif
__BVD_SER16550_H__
